<?php
//Database Requirement
define('HOST','localhost');
define('USER','root');
define('PASSWD','');
define('DATA_BASE','marocnews');
require_once 'dao/MySQLConnection.php';

function getArrayNewsbyCateg($categorie,$selected_source,$page){

    //config param
    $perpage=4;

    //generate Query
    $sqlwhere_01=" where categorie='".$categorie."'";
    $sqlwhere_02=" and source IN (";
    $i=0;
    foreach($selected_source as $sc){
        if($i==0) $sqlwhere_02.="'".$sc."'";
        else  $sqlwhere_02.=",'".$sc."'";
        $i++;
    }
    $sqlwhere_02.=")";
    $sqllimit_03=" LIMIT ".($page*$perpage).",$perpage";

    //Execute Query
    $_db=new MySQLConnection();
    $_db->connect();
    $sql="select * from news".$sqlwhere_01.$sqlwhere_02." order by date".$sqllimit_03;
    $res=$_db->executeQuery($sql);
    $t=array();
    $i=0;
    while($l=$_db->fetch_row_line($res))
    {
        $t[$i]=array('link'=>$l[0],'date'=>$l[1],'categorie'=>$l[2],'source'=>$l[3],'source_icon'=>$l[4],'photo'=>$l[5],'title'=>$l[6],'contenu'=>$l[7]);
        @$i++;
    }
    return $t;
}

    // Display JSON DATA , GET PARAMTER WITH $_GET
    echo json_encode(getArrayNewsbyCateg('general',array('hespress'),2));
?>